<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/3/17
 * Time: 10:00
 */

namespace app\admin\controller;


use app\admin\model\Active;
use app\admin\model\Reward;
use app\admin\model\RewardLog as AdminModel;
use think\Config;

class RewardLog extends Admin
{
    protected $code_name = '中奖记录';

    public function set_param(){
        parent::set_param();
        $this->assign('code','reward_log');
        $this->assign('code_name',$this->code_name);
        $this->admin_model = new AdminModel();
    }

    public function index()
    {
        $param = $this->request->param();
        $active_id = $param['active_id'];

        if(empty($active_id)){
            $this->error('活动id丢失');
        }
        $user_id = empty($param['user_id'])?'':$param['user_id'];
        $nickname = empty($param['nickname'])?'':$param['nickname'];
        $reward_id = empty($param['reward_id'])?'':$param['reward_id'];
        $where['active_id'] = $active_id;
        $config['query'] = ['active_id'=>$active_id];
        if($user_id){
            $where['user_id'] = ['like','%'.$user_id.'%'];
            $config['query']['user_id'] = $user_id;
        }
        if($nickname){
            $where['nickname'] = ['like','%'.$nickname.'%'];
            $config['query']['nickname'] = $nickname;
        }
        if($reward_id){
            $where['reward_id'] = ['=',$reward_id];
            $config['query']['reward_id'] = $reward_id;
        }
        $list = $this->admin_model->with('reward')->where($where)->order('reward_log.create_time desc')->paginate(10,false,$config);
        return view('',['list'=>$list,'page'=>$list->render(),'active_id'=>$active_id,'user_id'=>$user_id,'nickname'=>$nickname,'reward_id'=>$reward_id,
            'rewards'=>Reward::where('active_id',$active_id)->column('name','id')]);
    }

    public function receive(){
        $param = $this->request->param();
        if(empty($param['id'])){
            return json(['code'=>999,'message'=>'id丢失']);
        }

        $res = $this->admin_model->where('id',$param['id'])->where('is_receive',0)->update(['is_receive'=>1,'update_time'=>time()]);
        if($res){
            return json(['code'=>0,'message'=>'已设为领取']);
        }
        return json(['code'=>999,'message'=>'更新失败']);
    }

    public function to_excel(){

        $param = $this->request->param();
        $active_id = $param['active_id'];

        if(empty($active_id)){
            $this->error('活动id丢失');
        }
        $user_id = empty($param['user_id'])?'':$param['user_id'];
        $nickname = empty($param['nickname'])?'':$param['nickname'];
        $reward_id = empty($param['reward_id'])?'':$param['reward_id'];
        $where['active_id'] = $active_id;
        $config['query'] = ['active_id'=>$active_id];
        if($user_id){
            $where['user_id'] = ['like','%'.$user_id.'%'];
            $config['query']['user_id'] = $user_id;
        }
        if($nickname){
            $where['nickname'] = ['like','%'.$nickname.'%'];
            $config['query']['nickname'] = $nickname;
        }
        if($reward_id){
            $where['reward_id'] = ['=',$reward_id];
            $config['query']['reward_id'] = $reward_id;
        }
        $list = $this->admin_model->with('reward')->where($where)->order('reward_log.create_time desc')->select();

        $active_name = Active::where('id',$active_id)->value('name');
        require_once VENDOR_PATH . 'PHPExcel.php';
        $objPHPExcel = new \PHPExcel();


        // Add some data
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '奖品编号')
            ->setCellValue('B1', '奖品名称')
            ->setCellValue('C1', '中奖用户昵称')
            ->setCellValue('D1', '中奖用户手机')
        ;

        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);

        // Rename worksheet
        $objPHPExcel->getActiveSheet()->setTitle($active_name.'-活动奖品表');


        if($list){
            // Set active sheet index to the first sheet, so Excel opens this as the first sheet
            foreach($list as $key=>$value){
                $key +=2;
                $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('A'.$key, $value->id)
                    ->setCellValue('B'.$key, $value->reward->name)
                    ->setCellValue('C'.$key, $value->nickname)
                    ->setCellValue('D'.$key, $value->mobile);
            }

        }


        // Redirect output to a client’s web browser (Excel5)
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$active_name.'-活动奖品表.xls"');
        header('Cache-Control: max-age=0');
        // If you're serving to IE 9, then the following may be needed
        header('Cache-Control: max-age=1');

        // If you're serving to IE over SSL, then the following may be needed
        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header ('Pragma: public'); // HTTP/1.0

        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        exit;
    }
}